namespace DotNetNuke.UI.Utilities
{
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class Globals
    {
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Searches control hierarchy from top down to find a control matching the passed in name
        /// </summary>
        /// <param name="objParent">Root control to begin searching</param>
        /// <param name="strControlName">Name of control to look for</param>
        /// <returns></returns>
        /// <remarks>
        /// This differs from FindControlRecursive in that it looks down the control hierarchy, whereas, the 
        /// FindControlRecursive starts at the passed in control and walks the tree up.  Therefore, this function is 
        /// more a expensive task.
        /// </remarks>
        /// <history>
        /// [Jon Henning]	9/17/2004	Created
        /// [Jon Henning]   12/3/2004   Now checking if the control HasControls before calling FindControl.
        /// Using FindControl or accessing the controls collection on controls like
        /// the DataList can cause problems with ViewState
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Control FindControlRecursive(Control objParent, string strControlName)
        {
            return FindControlRecursive(objParent, strControlName, "");
        }

        public static Control FindControlRecursive(Control objParent, string strControlName, string strClientID)
        {
            Control objCtl = objParent.FindControl(strControlName);
            if (objCtl == null)
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = objParent.Controls.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        Control objChild = (Control) refObjectHelperL0.Current;
                        if (objChild.HasControls())
                        {
                            objCtl = FindControlRecursive(objChild, strControlName, strClientID);
                        }
                        if (((((objCtl != null) && (Strings.Len(strClientID) > 0)) && (objCtl.ClientID != strClientID)) ? 1 : 0) != 0)
                        {
                            objCtl = null;
                        }
                        if (objCtl != null)
                        {
                            return objCtl;
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            return objCtl;
        }

        public static string GetAttribute(Control objControl, string strAttr)
        {
            bool refBoolHelperL0 = true;
            if (refBoolHelperL0 == (objControl is WebControl))
            {
                return ((WebControl) objControl).Attributes[strAttr];
            }
            if (refBoolHelperL0 == (objControl is HtmlControl))
            {
                return ((HtmlControl) objControl).Attributes[strAttr];
            }
            return null;
        }

        public static void SetAttribute(Control objControl, string strAttr, string strValue)
        {
            string strOrigVal = GetAttribute(objControl, strAttr);
            if (Strings.Len(strOrigVal) > 0)
            {
                strValue = strOrigVal + strValue;
            }
            bool refBoolHelperL0 = true;
            if (refBoolHelperL0 == (objControl is WebControl))
            {
                WebControl objCtl = (WebControl) objControl;
                if (objCtl.Attributes[strAttr] == null)
                {
                    objCtl.Attributes.Add(strAttr, strValue);
                }
                else
                {
                    objCtl.Attributes[strAttr] = strValue;
                }
            }
            else if (refBoolHelperL0 == (objControl is HtmlControl))
            {
                HtmlControl objCtl = (HtmlControl) objControl;
                if (objCtl.Attributes[strAttr] == null)
                {
                    objCtl.Attributes.Add(strAttr, strValue);
                }
                else
                {
                    objCtl.Attributes[strAttr] = strValue;
                }
            }
        }
    }
}

